(:
	add analysis
 :)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display>{
let $aname := fn:tokenize($arguments[2],"/")[last()]
let $anitems := doc($arguments[2])/display/item
for $it in doc($arguments[1])/display/item
let $anchor := $it/info[@name="src-anchor"]/anchor
let $match := $anitems[info[@name="target-anchor"][1]/anchor = $it/info[@name="src-anchor"]/anchor][1]
return
<item>{
	(for $info in $it/info return $info,
	<info name="{$aname}">{$match/info[@name="comment"]/text()}</info>)
}</item>
}</display>

(:
  count turns
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ; 


<display>{

let $t := doc($arguments[1])/display
let $users := $t/item/info[@name="user"]/text()
let $usersdistinct := fn:distinct-values($users)
for $user in $usersdistinct
let $count := fn:count($t/item[info[@name="user"]/text() = $user])
order by $count descending
return
<item>
<info name="User">{$user}</info>
<info name="Total">{$count}</info>
</item>

}</display>


(:
	derniere-francois
 :)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display>{
 doc($arguments[1])/display/item[info[@name="user"]="francois"][last()]
}</display>(:
  A procedure to number all items in a Display file
  It suppresses any previous numbering
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display type='fr.emse.tatiana.analysis'>{
let $t := $arguments[1]
let $d := doc($t)/display/item
for $item at $p in $d
return
  <item>{
    <info name="target-anchor">{$item/info[@name="src-anchor"]/anchor}</info>,
    <info name="comment">{xs:string($item/info[position()=last()])}</info>
  }</item>
}</display>

(:
  export html
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
let $t := doc($arguments[1])/display
let $titles := fn:distinct-values($t/item/info/@name)
return
<html>
<body><table>{
<tr>{
for $title in $titles where fn:not($title = ("src-anchor","begin","end"))
return
<td>{$title}</td>
}</tr>,
let $d := $t/item
for $item at $p in $d
return
  <tr>{
       for $title in $titles where fn:not($title = ("src-anchor","begin","end"))
       return
         <td>{if ($title="time") then jj:dateFromDREW($item/info[@name=$title]/time/date)
         else xs:string($item/info[@name=$title])}</td>
  }</tr>
}</table></body></html>

(:
  find user
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ; 


<display>{

let $infoname := $arguments[2]
let $infovalue := $arguments[3]
let $t := $arguments[1]

for $item in doc($t)/display/item 
	where some $info in $item/info satisfies $info/@name = $infoname and $info/text()=$infovalue
	return $item


}</display>


(:
  find remove
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ; 


<display>{

let $t := $arguments[1]

for $item in doc($t)/display/item[info[@name="type"]="textboard"]
let $diff := xs:integer($item/info[@name="character-difference"]/integer/text())
	where $diff le 0
	return $item


}</display>


(:
  find patterns
:)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;

declare boundary-space strip;
declare variable $file := $arguments[1];
declare variable $facetnames := $arguments[2];
declare variable $sequence := $arguments[3];
declare variable $window := xs:integer($arguments[4]);
declare variable $seqlen := fn:count(fn:tokenize($sequence,">"));

declare function local:calculate-sequences ($context as item()*, $number as xs:integer, $current as element()) as item()* 
		{
			if ($number = 0) then $current (: stop recursion :)
			else if (fn:empty($context)) then () (: not enough to grant complete sequence :)
			else
				for $item at $pos in $context 
				let $newcurrent := <list>{
					($current/item,
					<item>{xs:string(jj:get-info($item,$facetnames))}</item>)}</list>
				return
					local:calculate-sequences($context[fn:position() > $pos], $number - 1, $newcurrent)
		};

<display>{
let $items := doc($file)/display/item


for $item at $pos in $items
	let $init := <list><item>{xs:string(jj:get-info($item,$facetnames))}</item></list>
	let $seqs := local:calculate-sequences($items[fn:position() = ($pos+1 to $pos+$window - 1)],$seqlen - 1,$init)
	let $prettyseqs := for $seq in $seqs 
	      let $stringseq := for $v in $seq/item return xs:string($v)
	      return string-join($stringseq, ' > ')
	where $prettyseqs = $sequence
	return $items[fn:position() = ($pos to $pos+$window - 1)]
 
}</display>

(:
  number user 
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
declare variable $doc := doc($arguments[1]);
declare variable $items := $doc/display/item;
 
<display>{
for $item in $items
return
<item>{
		for $info in $item/info where $info/@name != "num" return $info,
		  <info name="N">{1 + count($item/preceding-sibling::*[jj:get-info(.,"user")=jj:get-info($item,"user")])}</info>
  }</item>
}</display>


(:
  A procedure to detect patterns in a display file
:)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;

declare boundary-space strip;
declare variable $file := $arguments[1];
declare variable $facetnames := $arguments[2]; (:fn:tokenize($arguments[2],",");:)
declare variable $window := xs:integer($arguments[3]);
declare variable $seqlen := xs:integer($arguments[4]);

declare function local:calculate-sequences ($context as item()*, $number as xs:integer, $current as element()) as item()* 
		{
			if ($number = 0) then $current (: stop recursion :)
			else if (fn:empty($context)) then () (: not enough to grant complete sequence :)
			else
				for $item at $pos in $context 
				let $newcurrent := <list>{
					($current/item,
					<item>{xs:string(jj:get-info($item,$facetnames))}</item>)}</list>
				return
					local:calculate-sequences($context[fn:position() > $pos], $number - 1, $newcurrent)
		};

<display>{
let $items := doc($file)/display/item

let $sequences :=
	for $item at $pos in $items
	let $init := <list><item>{xs:string(jj:get-info($item,$facetnames))}</item></list>
	return local:calculate-sequences($items[fn:position() = ($pos+1 to $pos+$window - 1)],$seqlen - 1,$init)

let $uniqueseqs := fn:distinct-values($sequences)

let $values := for $item at $pos in $items return xs:string(jj:get-info($item,$facetnames))
let $uniquevals := fn:distinct-values($values) 
let $countvals := for $val in $uniquevals return fn:count($values[. eq $val])	
let $itemtotal := fn:count($values)


for $seq in $uniqueseqs
let $total := fn:count($sequences[. eq $seq])
let $sample := for $v in $sequences[. eq $seq][1]/item return xs:string($v)
let $pretty := string-join($sample, ' > ')
let $probs := ($countvals[fn:index-of($uniquevals,$sample[1])],
								  for $v in $sample[position()>1]
								  return $countvals[fn:index-of($uniquevals,$v)] * ($window - $seqlen + 1) div $itemtotal)
let $expected := jj:prod($probs,1)
let $delta := 100*($total - $expected) div $expected
order by fn:abs($delta) descending
return
  <item>{
    <info name="sequence">{$pretty}</info>,
    <info name="total">{$total}</info>,
    <info name="expected">{fn:round($expected)}</info>,
    <info name="difference">{fn:round($delta)}</info>
  }</item> 
}</display>


(:
	remove columns
 :)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display>{
let $t := $arguments[1]
let $vals := fn:tokenize($arguments[2],",")
for $it at $p in doc($arguments[1])/display/item
return
<item>{
  for $in in $it/info[fn:not(@name = $vals)]
  return
        $in
}</item>
}</display>
